package com.lichongbing.lyoggl.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.lichongbing.lyoggl.model.ReportMounth;
import com.lichongbing.lyoggl.service.MoneyService;
import com.lichongbing.lyoggl.utils.JwtTokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2022/8/10 16:00
 * @description: TODO
 */
@Slf4j
@RestController
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private MoneyService moneyService;

    /**
     * 今年月份报告
     *
     * @param mouth
     * @param request
     */
    @PostMapping("/{mouth}")
    public JSONObject reportMonth(@PathVariable String mouth, HttpServletRequest request) {
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        String userid = JwtTokenUtils.getUserid(token);
        String year = String.valueOf(DateUtil.thisYear());
        JSONObject jsonObject = new JSONObject();
        if (expiration == false) {
            JSONObject report = new JSONObject();
            ReportMounth reportMounth = moneyService.reportMounth(mouth,year,userid);
            if(reportMounth !=null){
                //收入
            report.put("money1", reportMounth.getMoney1());
                //支出
            report.put("money0", reportMounth.getMoney0());
                //最低还款
            report.put("pay1", reportMounth.getPay1());
                //费用
            report.put("pay2", reportMounth.getPay2());
                //可用额度
            report.put("duting", reportMounth.getDuting());
                // 负债
            report.put("duted", reportMounth.getDuted());
                // 现金
            report.put("balance", reportMounth.getBalance());
            jsonObject.put("code", 200);
            jsonObject.put("msg", "查询成功");
            jsonObject.put("data", report);
            return jsonObject;}
            else{
                report.put("money1", "0");
                report.put("money0", "0");
                report.put("pay1", "0");
                report.put("pay2", "0");
                report.put("duting", "0");
                report.put("duted", "0");
                jsonObject.put("code", 200);
                jsonObject.put("msg", "查询成功");
                jsonObject.put("data", report);
                return jsonObject;
            }
        } else {
            jsonObject.put("code", 400);
            jsonObject.put("msg", "token过期");
            return jsonObject;
        }
    }

    /**
     * 每日报告
     * @param day
     * @param month
     * @param userid
     * @return
     */
    private String  reportDay(String day,String month,String userid){
        String year = String.valueOf(DateUtil.thisYear());
        try {
           return moneyService.reportDay(day, year, month,userid);
        }catch (Exception e) {
            return "0";
        }
    }

    /**
     * 本月支出统计走势
     * @return
     */
    @PostMapping("/day/{mouth}")
    public JSONObject reportMounths(@PathVariable String mouth, HttpServletRequest request){
        List<JSONObject> days = new ArrayList<>();
        int today = DateUtil.thisDayOfMonth();
        String token = request.getHeader("token");
        boolean expiration = JwtTokenUtils.isExpiration(token);
        JSONObject jsonObject = new JSONObject();
        if (expiration == false) {
            String userid = JwtTokenUtils.getUserid(token);
            for (int i = 1; i <= today; i++) {
                JSONObject jsonObject1 = new JSONObject();
                String s = this.reportDay(String.valueOf(i), mouth, userid);
                jsonObject1.put(String.valueOf(i),s);
                days.add(jsonObject1);
            }
            jsonObject.put("code", 200);
            jsonObject.put("msg", "查询成功");
            jsonObject.put("data", days);
            return jsonObject;
        }else{
            jsonObject.put("code", 400);
            jsonObject.put("msg", "token过期");
            return jsonObject;
        }
    }


}
